package com.mes.biz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizProductInventory;
import com.mes.biz.domain.vo.InventoryVo;
import com.mes.biz.mapper.BizProductInventoryMapper;
import com.mes.biz.service.BizProductInventoryService;
import com.mes.common.core.page.PageDomain;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.sql.SqlUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BizProductInventoryServiceImpl extends BaseServiceImpl<BizProductInventoryMapper, BizProductInventory> implements BizProductInventoryService {
    @Resource
    private BizProductInventoryMapper bizProductInventoryMapper;
    @Override
    public void update(BizProductInventory bizProductInventory) {
        bizProductInventoryMapper.update(bizProductInventory);
    }

    @Override
    public BizProductInventory getByProductId(String productId) {
        return bizProductInventoryMapper.getByProductId(productId);
    }

    @Override
    public BizProductInventory getByProductIdAndSpaceId(String productCode, String spaceId) {
        return bizProductInventoryMapper.getByProductIdAndSpaceId(productCode, spaceId);
    }

    @Override
    public PageInfo<BizProductInventory> findDetailPage(BizProductInventory bizProductInventory) {
        PageDomain page = bizProductInventory.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(bizProductInventoryMapper.findDetailList(bizProductInventory));
    }

    @Override
    public PageInfo<InventoryVo> listInventoryStatPage(BizProductInventory bizProductInventory) {
        PageDomain page = bizProductInventory.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(bizProductInventoryMapper.listOrderStat(bizProductInventory));
    }

    @Override
    public List<InventoryVo> listInventoryStat(BizProductInventory bizProductInventory) {
        return bizProductInventoryMapper.listOrderStat(bizProductInventory);
    }
}
